<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar title="登录" />
    <!-- 表单 -->
    <!-- rules用来做表单校验写规则的地方 -->
    <!--
      submit事件
        按了回车、点击了提交（登录）按钮，而且还要等到整个表单校验通过才触发
     -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="info.mobile"
        label="手机号"
        placeholder="请输入手机号"
        :rules="rules.mobile"
      />
      <van-field
        v-model="info.code"
        label="验证码"
        placeholder="请输入验证码"
        :rules="rules.code"
      />
      <div style="margin: 16px;">
        <van-button
          :loading="isloading"
          loading-text="加载中..."
          round
          block
          type="info"
          native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  name: 'MyLogin',
  data () {
    return {
      isloading: false,
      info: {
        mobile: '13800138000',
        code: '246810'
      },
      rules: {
        mobile: [
          { required: true, message: '请填写手机号' },
          { pattern: /^1[2-9]\d{9}$/, message: '请填写正确的手机号' }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '请填写6位数字验证码' }
        ]
      }
    }
  },
  methods: {
    async onSubmit () {
      // 开启加载动画
      this.isloading = true
      // 异步
      await this.$store.dispatch('user/login', this.info)
      // 不管成功还是失败都会执行finally
      // 关闭加载动画
      this.isloading = false
    }
  }
}
</script>

<style></style>
